package myoa

import myoa.domain.RoleName
import myoa.tool.SessionTool
import myoa.tool.ShiroTool
import myoa.tool.StringTool

class ProjectController {

//----------level 1 by first access control: auth----------
    def create() {

    }

    def save() {
        Project p = new Project(params)
        p.traceCreate()
        if (p.save()) {
            ProUser proUser = new ProUser()                  //todo 事务
            proUser.user = SessionTool.loginUser
            proUser.project = p
            proUser.save()
            flash.message = message(code: 'default.save.success')
            redirect(action: 'list')
        } else {
            flash.message = message(code: 'default.save.failed')
            redirect(action: 'create')
        }
    }

//----------level 2 by following access control----------
    def show(Long id) {
        def p = Project.get(id)

        int empSize = 0
        p.proCompanies.each {
            empSize += it.proEmployees.size()
        }

        [project: p, comSize: p.proCompanies.size(), empSize: empSize]
    }

    def edit(Long id) {
        [project: Project.get(id)]
    }

    def update(Long id) {
        Project p = Project.get(id)
        p.properties = params
        p.traceUpdate()

        if (p.save()) {
            flash.message = message(code: 'default.save.success')
            redirect(action: 'show', id: id)
        } else {
            flash.message = message(code: 'default.save.failed')
            render(view: "edit", model: [project: p])
        }
    }

    def delete(Long id) {
        Project p = Project.get(id)
        p.delete()
        flash.message = message(code: 'default.delete.success', args: [p.name])
        redirect(action: 'list')
    }

//----------level 3 by action inner----------
    def list() {
        params.max = StringTool.paginateMax(params.max)
        if (params.sort == null) {      //默认排序  todo 不优美，但domain里的排序对where.query没用，只好这样
            params << [sort: 'lastUpdated', order: 'desc']
        }

        def projects = []  //todo empty return... any optimize?
        def total = 0
        if (ShiroTool.hasOneRoleOf(RoleName.MGR) || ShiroTool.isRoot()) {
            projects = Project.list(params)
            total = Project.count()
        } else if (ShiroTool.hasOneRoleOf(RoleName.EMP)) {
            def query = Project.where {
                /**
                 * todo 研究 spread collection field [root,test]
                 * http://groovy.codehaus.org/Operators#Operators-SpreadOperator
                 * todo 研究：域对象是默认对比ID的么？比的到底是什么.where里的操作符显然被重载过
                 */
                proUsers.user == SessionTool.loginUser
            }
            projects = query.list(params)
            total = query.count()

//            def testId = 2
//            println Project.get(testId).proUsers.user.name  //[test, root]
//            println SessionTool.loginUser == Project.get(testId).proUsers.user    //false
//            println SessionTool.loginUser in Project.get(testId).proUsers.user    //false
//            println SessionTool.loginUser.id == Project.get(testId).proUsers.user.id   //false
//            println SessionTool.loginUser.id in Project.get(testId).proUsers.user.id   //true
        }
        [projects: projects, total: total]
    }
}
